﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vara_Data_Layer.Vara_Data_Asset;
using Vara_Data_Layer.Vara_Data_Categories;

namespace AssetTracking.User_Controls.Asset_Module.Tabs
{
    public partial class Tab_2_Categories : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void Load_Interface(Vara_Data_IAsset AssetInformationToLoad)
        {
            LabelHiddenAssetID.Text = AssetInformationToLoad.ID.ToString();
            PopulateGridviewCatgories(AssetInformationToLoad);
            PopulateDropDownListParentCategories();
        }
        //Returns 
        protected void PopulateDropDownListParentCategories()
        {
            DropDownList_Parent_Categories.DataSource = new Vara_Data_Category_Controller().Categories;
            DropDownList_Parent_Categories.DataTextField = "Name";
            DropDownList_Parent_Categories.DataValueField = "ID";
            DropDownList_Parent_Categories.DataBind();
        }
        protected void DropDownList_Parent_Categories_SelectedIndexChanged(object sender, EventArgs e)
        {
            Vara_Data_SubCategory_Controller VaraSubCategoryController = new Vara_Data_SubCategory_Controller();
            Vara_Data_Category_Controller VaraCategoryController = new Vara_Data_Category_Controller();
            Vara_Data_ICategory CategorySelected = VaraCategoryController.ReturnCategory(int.Parse(DropDownList_Parent_Categories.SelectedItem.Value));
            DropDownList_Sub_Categories.DataSource = VaraSubCategoryController.ReturnSubCategoriesByParent(CategorySelected);
            DropDownList_Sub_Categories.DataTextField = "Name";
            DropDownList_Sub_Categories.DataValueField = "ID";
            DropDownList_Sub_Categories.DataBind();
        }
        protected void PopulateGridviewCatgories(Vara_Data_IAsset AssetInformationToLoad)
        {
            Vara_Data_SubCategory_Controller VaraSubCategoryController = new Vara_Data_SubCategory_Controller();
            GridView_Categories.DataSource = VaraSubCategoryController.ReturnAllSubCategoriesByAsset(AssetInformationToLoad.ID);
            GridView_Categories.DataBind();
        }

        protected void GridviewCategoriesCommandControls(object sender, EventArgs e)
        {
            GridViewRow GridViewCurrentRow = ((GridViewRow)((ImageButton)sender).NamingContainer);
            ImageButton ImageButtonSent = ((ImageButton)sender);

            if (ImageButtonSent.CommandName == "DeleteAssetSubCategory")
            {
                Vara_Data_SubCategory_Controller VaraSubCategoryController = new Vara_Data_SubCategory_Controller();
                VaraSubCategoryController.DeleteAssetSubCategory(int.Parse(GridView_Categories.DataKeys[GridViewCurrentRow.RowIndex].Values[0].ToString()));         
            }
            Vara_Data_Asset_Controller AssetController = new Vara_Data_Asset_Controller();
            Vara_Data_IAsset AssetToReturn = AssetController.ReturnAsset(int.Parse(LabelHiddenAssetID.Text.ToString()));
            PopulateGridviewCatgories(AssetToReturn);
        }
        protected void ButtonAddCategory_Click(object sender, EventArgs e)
        {
            Vara_Data_SubCategory_Controller VaraSubCategoryController = new Vara_Data_SubCategory_Controller();
            VaraSubCategoryController.InsertAssetSubCategory(int.Parse(LabelHiddenAssetID.Text.ToString()), int.Parse(DropDownList_Sub_Categories.SelectedItem.Value));
            Vara_Data_Asset_Controller AssetController = new Vara_Data_Asset_Controller();
            Vara_Data_IAsset AssetToReturn = AssetController.ReturnAsset(int.Parse(LabelHiddenAssetID.Text.ToString()));
            PopulateGridviewCatgories(AssetToReturn);
        }
    }
}